
package com.eworking.eim.user;

import com.eworking.eim.EIMConfig;

public class EIMAccountUtils {
    private final static String SPLIT_CHAR = "#";
    /**
     * 根据用户登陆账号获取企业账号
     * 
     * @param userAccount 用户账号
     * @return
     */
    public static String getBareJIDByAccount(String account) {
        String strAppKey = EIMConfig.getInstance().getEntCode();
        return strAppKey + SPLIT_CHAR + account;
    }
    
    /**
     * 根据企业账号获取用户账号
     * 
     * @param JID 企业账号
     * @return 用户账号
     */
    public static String getAccountFromJID(String JID) {
        // 企业账号 entcode_account
        String bareJID = "";
        if (JID.contains("@")) {
            bareJID = JID.substring(0, JID.indexOf("@"));
        } else {
            bareJID = JID;
        }
        // 用户账号
        if (bareJID.startsWith(EIMConfig.getInstance().getEntCode())) {
            bareJID = bareJID.substring((EIMConfig.getInstance().getEntCode() + SPLIT_CHAR).length());
        }
        return bareJID;
    }
    
    /**
     * 根据用户账号获取企业账号
     * 
     * @param account 用户账号
     * @return 用户JID
     */
    public static String getJIDFromAccount(String account) {
        if (account.contains("@")) {
            return account;
        }
        return EIMConfig.getInstance().getEntCode() + SPLIT_CHAR + account + "@" + EIMConfig.DOMAIN;
    }
    
    /**
     * 根据企业账号获取群组ID
     * 
     * @param JID
     * @return
     */
    public static String getGroupIdFromGroupJID(String JID) {
        String groupId;
        if (JID.contains("@")) {
            groupId = JID.substring(0, JID.indexOf("@"));
        } else {
            groupId = JID;
        }
        if (groupId.startsWith(EIMConfig.getInstance().getEntCode())) {
            return groupId.substring((new StringBuilder(EIMConfig.getInstance().getEntCode())).append(SPLIT_CHAR).toString()
                .length());
        } else {
            return groupId;
        }
    }
    
    /**
     * 根据群组ID获取企业群组ID
     * 
     * @param groupId
     * @return
     */
    public static String getGroupJIDFromGroupId(String groupId) {
        if (groupId.contains("@")) {
            return groupId;
        }
        return EIMConfig.getInstance().getEntCode() + SPLIT_CHAR + groupId + EIMConfig.MUC_DOMAIN_SUFFIX;
    }
}
